<%= javascript_tag render :partial => 'tt_overview/time_tracker', :formats => [:js] %>
<%
   activities = time_tracker.project_id.nil? ? TimeEntryActivity.shared.active.all : Project.visible.where(:id => time_tracker.project_id).first().activities
   options = options_from_collection_for_select(activities, :id, :name).split("\n").join("")
   select = select_tag :tt_activity, options, {:include_blank => true, :onchange => 'updateActivity()'}
   centered = content_tag :div, select, :class => 'center'
   desc = content_tag :p, l(:tt_activity_dialog_description)
%>
<script type="text/javascript">
    function updateActivity(val) {
        if (typeof(val) === 'undefined') {
            val = $('#tt_activity').val();
        } else {
            $('#tt_activity').val(val);
        }
        $.ajax({url: base_url() + '/time_trackers/update?key=<%= User.current.api_key %>&time_tracker[activity_id]=' + val,
            type: 'PUT'
        });
        $('#tt_activity_dialog_stop').button("option", "disabled", val == '');
    }
    function showActivityDialog(start_new) {
        var $dialog = $('#activity_dialog')
        if ($dialog.length == 0) {
            $('<div/>', {id: 'activity_dialog', title: '<%= l(:tt_activity_dialog_title)%>'})
                    .appendTo('body')
                    .append('<%=content_tag :div, desc + centered%>')
                    .dialog({
                        autoOpen: true,
                        resizable: false,
                        draggable: false,
                        modal: true,
                        buttons: [
                            {
                                id: "tt_activity_dialog_stop",
                                text: '<%=stop_title%>',
                                disabled: true,
                                click: function () {
                                    $(this).dialog("close");
                                }
                            },
                            {
                                text: "<%= l(:button_cancel)%>",
                                click: function () {
                                    $(this).dialog("close");
                                    updateActivity('<%= time_tracker.activity_id%>');
                                }
                            }
                        ],
                        create: function () {
                            if (start_new) {
                                $('#tt_activity_dialog_stop').wrap('<%=link_to '',{:controller => 'time_trackers', :action => 'stop', :start_new_time_tracker => (@issue.nil? ? '' : @issue.id)},:data => {:type => 'html'}, :class => 'tt_activity_dialog_stop_link', :remote => remote%>');
                            } else {
                                $('#tt_activity_dialog_stop').wrap('<%=link_to '',{:controller => 'time_trackers', :action => 'stop'},:data => {:type => 'html'}, :class => 'tt_activity_dialog_stop_link', :remote => remote%>');
                            }
                        }
                    })
        } else {
            $dialog.dialog('open')
        }

    }
    $(document).ready(function () {
        $(document).on('click', '.tt_stop', function (e) {
            showActivityDialog(false);
            if (typeof(contextMenuHide) === typeof(Function)) {
                contextMenuHide();
            }
            e.preventDefault();
        });
        $(document).on('click', '.tt_start_dialog_stop', function (e) {
            showActivityDialog(true);
            if (typeof(contextMenuHide) === typeof(Function)) {
                contextMenuHide();
            }
            e.preventDefault();
        });
        $(document).on('click', '.tt_activity_dialog_stop_link', function (e) {
            if ($('#tt_activity_dialog_stop').button("option", "disabled") == true) {
                e.preventDefault();
            }
        });
    });
</script>
